/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elementsDefense.enemies;

import com.jme3.app.state.AbstractAppState;
import com.jme3.asset.AssetManager;
import com.jme3.scene.Node;
import elementsDefense.EBus;
import elementsDefense.gui.HUD;
import java.util.ArrayList;

/**
 *
 * @author Ricardo
 */
public class EnemyWave extends AbstractAppState {
    enum State{TIME_WAIT,SPAWNING,ENEMY_WAIT};
    State currentState= State.TIME_WAIT;
    
    AssetManager assetManager;
    Node rootNode;
    
    public static ArrayList<BasicEnemy> enemies = new ArrayList<BasicEnemy>();
    public int wave=1;
    public float waitTime=10;
    public int enemiesSpawned=0;

    public EnemyWave(AssetManager assetManager, Node rootNode) {
        this.assetManager=assetManager;
        this.rootNode = rootNode;
    }
    
    

    float passed=0;
    @Override
    public void update(float tpf) {
        if(isEnabled()){
            passed+=tpf;
            switch(currentState){
                case TIME_WAIT: 
                    if(passed>waitTime) {
                        EBus.firePropertyChange(EBus.Property.WAVE_NUMBER, wave-1, wave);
                        currentState=State.SPAWNING;
                        passed=0;
                        enemiesSpawned=0;
                    }else{
                        EBus.firePropertyChange(EBus.Property.WAVE_COUNTDOWN, 0, (int)(waitTime-passed));
                    }
                    break;
                case SPAWNING:
                    if(enemiesSpawned<(3+wave*2)){
                        if(passed>.3){
                        spawn();
                        passed-=.3;}
                    }else{
                        currentState=State.ENEMY_WAIT;
                    }
                    break;
                case ENEMY_WAIT: 
                    if(enemies.isEmpty()) {
                        currentState=State.TIME_WAIT;
                        passed=0;
                        wave++;
                    }
            }
        }
    }
    
    public void spawn(){
        BasicEnemy basicEnemy = new BasicEnemy();
        basicEnemy.build(assetManager, rootNode);
        enemies.add(basicEnemy);
        enemiesSpawned++;
        EBus.firePropertyChange(EBus.Property.ENEMY_COUNT, enemies.size()-1, enemies.size());
    }
    
    public static void remove(BasicEnemy enemy){
        enemies.remove(enemy);
        EBus.firePropertyChange(EBus.Property.ENEMY_COUNT, enemies.size()+1, enemies.size());
    }
    
    
}
